// JavaScript Document
var STGestureSlider = function( DIV_ViewStage )
{
	var _this 					= this;
	var _nxSlider 				= new STViewSlider( DIV_ViewStage );
	var _sContainer 			= _nxSlider.getContainer();
	_nxSlider.onTweenComplete	= _onTweenComplete;
	
	// [ 제스쳐 처리 ]
	var _gestureCapture;
	// 스와이핑 제스쳐
	_gestureCapture				= new STGestureCapture( DIV_ViewStage );
	_gestureCapture.onStart 	= onStartGesture;
	_gestureCapture.onUpdate 	= onUpdateGesture;
	_gestureCapture.onEnd 		= onEndGesture;
	
	var _slides;
	var _pIndex;
	this.setSlides = function( pageList )
	{
		_slides = pageList;
		_nxSlider.clear();
		_pIndex = 0;
		// 기본값 설정
		if( _slides.length == 0 ) return;
		_nxSlider.push( _slides[ 0 ] );
		
	}
	
	// 콜백
	this.onTweenComplete;
	function _onTweenComplete()
	{
		_this.onTweenComplete();
	}
	this.currentPage = function()
	{
		if( parseFloat( _sContainer.style.left ) >= 0 && _sContainer.clientWidth > _nxSlider.getStage().clientWidth )
		{
			return _nxSlider.length()-1;
		}
		return _nxSlider.length();
	}
	this.totalPages = function()
	{
		return _slides.length;
	}
	
	// 버튼 이벤트
	this.prev = function()
	{
		_pIndex--;
		updatePageView();
		
	}
	this.next = function( )
	{
		_pIndex++;
		updatePageView();
	}
	function updatePageView()
	{
		var _isAnimation = arguments[0] != false;
		validatePageIndex();
		while( _pIndex < _nxSlider.length()-1 )
		{
			if( _nxSlider.length() <= 1 ) return;
			_nxSlider.pop(_isAnimation);
		}
		while( _pIndex > _nxSlider.length()-1 )
		{
			if( _slides.length <= 0 || _slides.length == _nxSlider.length() ) return;
			_nxSlider.push( _slides[ _nxSlider.length() ] ,_isAnimation );
		}
	}
	function validatePageIndex()
	{
		if( _pIndex < 0 ) _pIndex = 0;
		if( _pIndex > _slides.length-1 ) _pIndex = _slides.length-1;
	}
	
	
	
	this.setPageIndex = function( value )
	{
		_pIndex = value;
		updatePageView();
	}
	
	// [ 제스쳐 리스너 ]
	var _sWidth;
	var _sHalf;
	var _cx;
	function onStartGesture( vx, vy )
	{
		// _sContainer.style.left 는 0 상태임
		_sWidth = _nxSlider.getStage().clientWidth;
		_sHalf	= _sWidth/2;
		STTween.clear( _sContainer );
		//console.log( "[ onStartGesture ]", _pIndex );
	}
	function onUpdateGesture( vx, vy )
	{
		
		_cx = parseFloat( _sContainer.style.left );
		
		var _tx = (_cx+vx);
		if( _pIndex <= 0 && vx > 0 )
		{
			// 첫번째 페이지
			_tx -= vx/2;
		}else if( _pIndex == _slides.length-1 && vx < 0 )
		{
			// 마지막 페이지
			_tx -= vx/2;
		}else
		{
			if( _tx < -_sWidth )
			{
				// 좌측 한계점을 벗어남
				if( _slides.length > _nxSlider.length() )
				{
					_tx += _sWidth;
					if( _pIndex == _nxSlider.length() )
					{
						// 트윈중 반대로 방향전환시에 생기는 인덱스 꼬임 현상에 대한 예외 처리
						_nxSlider.push( _slides[ _nxSlider.length() ], false );
					}
					_nxSlider.push( _slides[ _nxSlider.length() ], false );
				}else{
					_tx -= vx/2;
				}
			}else if( _tx > 0 )
			{
				// 우측 한계점을 벗어남
				if( _nxSlider.length() > 1 )
				{
					while( _pIndex < _nxSlider.length()-1 )
					{
						// 트윈중 반대로 방향전환시에 생기는 인덱스 꼬임 현상에 대한 예외 처리
						_nxSlider.pop( false );
					}
					_tx -= _sWidth;
					_nxSlider.pop( false );
				}else{
					_tx -= vx/2;
				}
			}else{
				
				// 그외 페이지가 있는데, 안붙어 있을 경우
				if( _tx < 0 && _sContainer.clientWidth <= _sWidth && _slides.length > _nxSlider.length()  )
				{
					_nxSlider.push( _slides[ _nxSlider.length() ], false );
				}
				
			}
			
		}
		
		// 중심 체크
		if( _nxSlider.getContentOfPageB() != null )
		{
			if( _tx < -_sHalf )
			{
				_pIndex = _slides.indexOf( _nxSlider.getContentOfPageB() );
			}else{
				_pIndex = _slides.indexOf( _nxSlider.getContentOfPageA() );
			}
		}else{
			_pIndex = _slides.indexOf( _nxSlider.getContentOfPageA() );
		}
		_sContainer.style.left = _tx+"px";
		validatePageIndex();
		//console.log( "[ onUpdateGesture ] 설정될 페이지 인덱스 : ", _pIndex);
	}
	function onEndGesture( vx, vy, type )
	{
		if( _slides.length <= 1 )
		{
			tweenHorizontal( 0 );
			return;
		}
		_cx = parseFloat( _sContainer.style.left );
		
		// 중심 체크
		if( type == STGestureCaptureType.SWIPE_LEFT && _pIndex < _slides.length-1 )
		{
			if( _cx  > -_sHalf ) _pIndex++;
			//console.log( "왼쪽으로:", _pIndex );
			tweenHorizontal( -_sWidth );
		}else if( type == STGestureCaptureType.SWIPE_RIGHT && _pIndex > 0 )
		{
			if( _cx  < -_sHalf ) _pIndex--;
			//console.log( "오른쪽으로:", _pIndex );
			tweenHorizontal( 0 );
		}else{
			//console.log( "원위치로" , _pIndex);
			// 근접한 곳으로
			if( _cx > -_sHalf || ( _cx < -_sHalf && _pIndex == _slides.length-1 && _sContainer.clientWidth <= _sWidth)) 
			{
				// 다시 오른쪽으로 돌아감
				tweenHorizontal( 0 );
			}else{
				// 다시 왼쪽으로 돌아감
				tweenHorizontal( -_sWidth );
			}
		}
		
	}
	function tweenHorizontal( _x )
	{
		STTween.clear( _sContainer );
		// 효과
		STTween.to( _sContainer , 0.36, {left:_x, ease:STEaseLite.easeOutCubic, onComplete: onRemovedContent } );
	}
	
	function onRemovedContent()
	{
		updatePageView( false );
		_nxSlider.refreshPages();
	}
}